package com.mobileares.midp.widgets.client.tab;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import com.mobileares.midp.widgets.client.common.LI;
import com.mobileares.midp.widgets.client.common.UL;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-3-24
 * Time: 15:06:56
 * To change this template use File | Settings | File Templates.
 */
public class CTabPanel extends Composite implements ITab {
    private UL ul;
    private int selected = -1;
    private Map<Integer, Widget> cache = new HashMap<Integer, Widget>();
    private SimplePanel main;

    public CTabPanel() {
        FlowPanel wrapper = new FlowPanel();
        SimplePanel panel = new SimplePanel();
        panel.setStyleName("tab-ctab");
        ul = new UL();
        panel.add(ul);

        main = new SimplePanel();
        main.setStyleName("tab-ctab-content");
        wrapper.add(panel);
        wrapper.add(main);
        initWidget(wrapper);
    }

    private Integer addTabItem(String text) {
        final LI li = new LI();
        li.setStyleName("item");
        li.addStyleDependentName("normal");
        InlineLabel sp = new InlineLabel(text);
        sp.setStyleName("tab-span");
        sp.addStyleDependentName("normal");
        sp.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                SelectionEvent.fire(CTabPanel.this, ul.getWidgetIndex(li));
                setSelected(ul.getWidgetIndex(li));
            }
        });
        li.add(sp);
        ul.add(li);
        return ul.getWidgetCount() - 1;
    }

    public void addItem(String text, Widget widget) {
        cache.put(addTabItem(text), widget);
    }

    public void setSelected(int selected) {
        //todo:
        if (this.selected != -1) {
            LI widget = (LI) ul.getWidget(this.selected);
            widget.removeStyleDependentName("selected");
            widget.addStyleDependentName("normal");
            widget.getWidget(0).removeStyleDependentName("selected");
            widget.getWidget(0).addStyleDependentName("normal");
        }
        ul.getWidget(selected).removeStyleDependentName("normal");
        ul.getWidget(selected).addStyleDependentName("selected");
        ((LI) ul.getWidget(selected)).getWidget(0).removeStyleDependentName("normal");
        ((LI) ul.getWidget(selected)).getWidget(0).addStyleDependentName("selected");
        this.selected = selected;
        main.setWidget(getItem(selected));
    }

    public Widget getItem(int index) {
        return cache.get(index);
    }

    /**
     * Adds a {@link com.google.gwt.event.logical.shared.SelectionEvent} handler.
     *
     * @param handler the handler
     * @return the registration for the event
     */
    public HandlerRegistration addSelectionHandler(SelectionHandler<Integer> handler) {
        return addHandler(handler, SelectionEvent.getType());
    }
}
